package cn.buddha.jec.mvc.advice;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;

/**
 * @ClassName TongAdviceLog
 * @Author 友野浩二
 * @Date 2020/4/14 14:06
 * @Description
 * @Version 1.0
 */
@Slf4j
public class TongAdviceLog {

    /**
     * @return java.lang.String
     * @Author 友野浩二
     * @Description // 打印http请求/响应的body体数据
     * @Date 2020/4/13 22:27
     * @Param [parameter, body, decrypt]
     */
    public static String logHttpBodyData(final MethodParameter parameter, final String httpBodyStr,
                                         final String logInfo) {
        StringBuilder URI = new StringBuilder();

        Method method = parameter.getMethod();
        if (method != null) {
            RequestMapping requestMappingClass = parameter.getDeclaringClass().getAnnotation(RequestMapping.class);
            if (requestMappingClass != null) {
                String[] uriValue = requestMappingClass.value();
                if (uriValue.length > 0) {
                    URI.append(requestMappingClass.value()[0]);
                }
            }
            RequestMapping requestMappingMethod = method.getAnnotation(RequestMapping.class);
            if (requestMappingMethod != null) {
                String[] uriValue = requestMappingMethod.value();
                if (uriValue.length > 0) {
                    URI.append(requestMappingMethod.value()[0]);
                }
            }

            log.info(logInfo,
                    URI,
                    System.lineSeparator(),
                    method.getDeclaringClass().getName(),
                    method.getName(),
                    System.lineSeparator() + httpBodyStr);
        }

        return URI.toString();
    }

}
